package com.ljfiction.book.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.DengjiHuodeBean;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Android on 2018/2/1.
 */

public class HuoquJifenFragment extends BaseFragment {


    private RecyclerView recycleDengjiguize;
    private TextView guize;
    private SmartRefreshLayout refreshGuize;

    private BaseQuickAdapter mAdapter;
    private Context context;
    private List<DengjiHuodeBean.DataBean.SorceInfoBean> list = new ArrayList<>();
    private DengjiHuodeBean dengjiHuodeBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dengjiguize_fragment, null);
        recycleDengjiguize=view.findViewById(R.id.recycle_dengjiguize);
        guize=view.findViewById(R.id.guize);
        refreshGuize=view.findViewById(R.id.refresh_guize);
       refreshGuize.setEnableLoadMore(false);
        context = getContext();
        intView();
        intData();


        return view;
    }

    private void intView() {
        recycleDengjiguize.setAdapter(mAdapter = new BaseQuickAdapter<DengjiHuodeBean.DataBean.SorceInfoBean, BaseViewHolder>(R.layout.dengjiguize_item,list) {
            @Override
            protected void convert(BaseViewHolder helper, DengjiHuodeBean.DataBean.SorceInfoBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.dengjiguize_title, item.getTitle()).setText(R.id.dengjiguize_value, item.getValues());
            }
        });
        recycleDengjiguize.setLayoutManager(new LinearLayoutManager(context));
        recycleDengjiguize.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        refreshGuize.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                intData();
            }
        });
    }

    private void intData() {
        OkHttpUtils.get(HttpManager.IP + "/getUserLevenInfoSource")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        refreshGuize.finishRefresh();
                        Gson gson = new Gson();
                        try {
                            dengjiHuodeBean = gson.fromJson(s, DengjiHuodeBean.class);
                            if (dengjiHuodeBean.getReturnCode() == 200) {
                                list.clear();
                                list.addAll(dengjiHuodeBean.getData().getSorceInfo());
                                mAdapter.notifyDataSetChanged();
                                guize.setText(dengjiHuodeBean.getData().getExplainInfo());
                            } else {
                                showToast(dengjiHuodeBean.getMsg());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }



}
